<?php
class Publicidad extends Doctrine_Record {
    public function setTableDefinition() {
        $this->setTableName('publicidad');
        $this->hasColumn('id', 'integer', 6, array('primary' => true, 'unsigned'=>true, 'autoincrement'=>true));
        $this->hasColumn('descripcion', 'string', 255);
        $this->hasColumn('src', 'string', 255);
        $this->hasColumn('url', 'string', 255);
        $this->hasColumn('orden', 'integer', 3, array('default'=>0, 'unsigned'=>true));
        $this->hasColumn('clicks', 'integer', 20, array('default'=>0, 'unsigned'=>true));
        $this->hasColumn('impresiones', 'integer', 20, array('default'=>0, 'unsigned'=>true));
        $this->hasColumn('id_tipo', 'integer', 1, array('unsigned'=>true));
        $this->hasColumn('id_estado', 'integer', 1, array('unsigned'=>true));
    }
    
    public function setUp(){
        $this->hasOne('TipoPublicidad as tipo',array(
            'local'=>'id_tipo',
            'foreign'=>'id'
        ));
        $this->hasOne('Estado as estado',array(
            'local'=>'id_estado',
            'foreign'=>'id'
        ));
        $this->hasMany('Publicacion as publicaciones', array(
            'local' => 'id_publicidad',
            'foreign' => 'id_publicacion',
            'refClass' => 'RelPublicidadPublicacion'
        ));
        $this->hasMany('Categoria as categorias', array(
            'local' => 'id_publicidad',
            'foreign' => 'id_categoria',
            'refClass' => 'RelPublicidadCategoria'
        ));
        //behaviors
        $this->actAs('Sluggable', array('fields'=>array('descripcion'),'unique'=>true,'canUpdate'=>true,'name'=>'slug'));
    }
    
    public static function lastId () {
        $q = Doctrine_Query::create()
                ->select('p.id')
                ->from('Publicidad p')
                ->orderBy('p.id desc')
                ->limit(1)
                ->offset(0);
        $result = $q->fetchOne(array(), Doctrine::HYDRATE_SINGLE_SCALAR);
        return $result;
    }
    
}
?>